package com.kuiandjie.web;


import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.kuiandjie.entity.Employee;
import com.kuiandjie.entity.Files;
import com.kuiandjie.entity.Project;
import com.kuiandjie.service.EmployeeService;
import com.kuiandjie.service.FileService;
import com.kuiandjie.util.DateUtil;
import com.kuiandjie.util.UUUtil;

public class HeadServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if("GET".equals(request.getMethod())){
			request.getRequestDispatcher("WEB-INF/views/head.jsp").forward(request, response);
		}else{
			HttpSession session = request.getSession();
			Employee emp = (Employee) session.getAttribute("employee");
			Project p=  (Project) session.getAttribute("curp");
			String path = getServletContext().getRealPath("/");
			File file = new File(path + "/head");
			File temp = new File("d:/img");
			if(!file.exists()) {
				file.mkdir();
			}
			
			if(!temp.exists()) {
				temp.mkdir();
			}
			
			if(ServletFileUpload.isMultipartContent(request)){
				//设置磁盘文件工厂，缓存大小，缓存文件夹
				DiskFileItemFactory dif = new DiskFileItemFactory();
				dif.setSizeThreshold(1024);
				dif.setRepository(temp);
				
				ServletFileUpload servletFileUpload = new ServletFileUpload(dif);
				//单个文件的上传体积限制
				servletFileUpload.setSizeMax(1024*100*1024);
				
				try {
					List<FileItem> list = servletFileUpload.parseRequest(request);
					if(list.size()==0 || list == null){
						response.sendRedirect("head.jspx" );
						return;
					}
					
					for(FileItem item : list){
						if(item.isFormField()){
							//接收不是文件类型的数据
						}else{
							if(item.getSize() > 1024*100){
								
								response.sendRedirect("head.jspx");
								
								return;
							}
							
							//判断是不是没有选择文件直接点击上传
							if(item.getName() == ""){
								response.sendRedirect("head.jspx");
								return;
							}
							//截取文件的后缀str
							String str = "";
							if(item.getName().indexOf(".") > 0){
								 str = item.getName().substring(item.getName().lastIndexOf(".")+1).toLowerCase();
								 if("jepg".equals(str) ||"jpg".equals(str) || "png".equals(str) || "gif".equals(str)){
									 String picName = emp.getId() + "."+str;
									 item.write(new File(file,picName));
									
									 EmployeeService es = new EmployeeService();
									 emp.setPic(picName);
									 es.uploadHead(emp.getId(),emp.getPic());
									 if(p == null){
										 response.sendRedirect("main.jspx");
									 }else{
										 response.sendRedirect("project.jspx?pid="+p.getId());
									 }
									 
								 }else{
									 response.sendRedirect("head.jspx");
								 }
							}else {
								response.sendRedirect("head.jspx");
								
							}
							
							
							
						}
					}
					
				} catch (FileUploadException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
	}

}
